﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace WpfApp1.ViewModel
{
    public class GrammerViewModel : ObservableObject
    {
        public RelayCommand<object> EqualsTestCommand { get; set; }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; this.RaisePropertyChanged(nameof(Name)); }
        }
        private bool isTrigger;
        public bool IsTrigger
        {
            get { return isTrigger; }
            set { isTrigger = value; this.RaisePropertyChanged(nameof(IsTrigger)); }
        }
        public GrammerViewModel()
        {
            name = "123";
            RectHeight = 40;
            EqualsTestCommand = new RelayCommand<object>((b) => { IntTest(); });
            ClosePackageCommand = new RelayCommand<object>(async (b) => { await ClosePackage(); });
            RefCommand = new RelayCommand<object>((object b) =>
            {

                RefTest();
            });
            ThreadCommand = new RelayCommand<object>((object b) =>
            {
                ThreadTest();

            });
            TopDisCommand = new RelayCommand<object>((object b) => {

                //TopDis -= 10;
                //RectHeight += 10;
                IsTrigger = true;



            });
            TopDownDisCommand = new RelayCommand<object>((object b) => {

                TopDis += 10;
                RectHeight -= 10;
            });
        }

        #region ==和equals
        public void IntTest()
        {
            //==
            int a = 2;
            int c = 2;
            float b = 2.0f;
            Debug.WriteLine($"int和float结果{a == b}");
            Human human1 = new Human() { Name = "zs", Age = 12 };
            Human human2 = new Human() { Name = "zs", Age = 12 };
            Human human3 = human1;
            human3.Age = 13;
            Debug.WriteLine($"human1和human2：{human1 == human2}");
            Debug.WriteLine($"human1和human3：{human1 == human3}");

            //equals
            Debug.WriteLine("Equals比较");
            Debug.WriteLine($"a和b：{a.Equals(b)}");
            Debug.WriteLine($"a和c：{a.Equals(c)}");
            Debug.WriteLine($"human1和human2：{human1.Equals(human2)}");
            Debug.WriteLine($"human1和human3：{human1.Equals(human3)}");

        }
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        #endregion


        #region aciton fun
        public RelayCommand<object> CallBackCommand { get; set; } = new RelayCommand<object>((t) =>
        {

        });

        public void FuncA(Action<string> action)
        {
            action("name");
        }
        public void FuncA(Func<string> action)
        {
            action();
        }
        public string GetGreeting(string name)
        {
            return name;
        }
        public void FuncB(string name)
        {
            Console.WriteLine($"{name}");
        }
        public string FuncC(string name)
        {
            return name;
        }
        public void CallBack()
        {

            FuncA((s) => { FuncB("name" + s); });
            FuncA(() => { return GetGreeting("name"); });
        }
        #endregion

        #region 闭包
        public RelayCommand<object> ClosePackageCommand { get; set; }

        public async Task ClosePackage()
        {
            Debug.WriteLine($"时间：{DateTime.Now.ToString("dd-HH:mm:ss:ffff")}");
            for (int i = 0; i < 10; i++)
            {
                Debug.WriteLine($"进入方法：{DateTime.Now.ToString("dd-HH:mm:ss:ffff")}");
                await Task.Run(() =>
                {
                    Debug.WriteLine($"{i}：{DateTime.Now.ToString("dd-HH:mm:ss:ffff")}");
                });
            }
            //int counter = 0; // 初始化计数器变量

            //// 创建第一个线程，用于增加计数器的值
            //Thread thread = new Thread(() =>
            //{
            //    // 线程A的执行逻辑：增加计数器的值并输出
            //    for (int i = 0; i < 10; i++)
            //    {
            //        counter++; // 增加计数器的值
            //        Console.WriteLine($"线程A：计数器 = {counter}");
            //        Thread.Sleep(100); // 暂停100毫秒
            //    }
            //});

            //thread.Start(); // 启动线程A

            //// 创建第二个线程，用于减少计数器的值
            //Thread threadB = new Thread(() =>
            //{
            //    // 线程B的执行逻辑：减少计数器的值并输出
            //    for (int i = 0; i < 10; i++)
            //    {
            //        counter--; // 减少计数器的值
            //        Console.WriteLine($"线程B：计数器= {counter}");
            //        Thread.Sleep(150); // 暂停150毫秒
            //    }
            //});

            //threadB.Start(); // 启动线程B

            //thread.Join(); // 等待线程A结束
            //threadB.Join(); // 等待线程B结束

            //Console.WriteLine("主线程正在退出。"); // 输出主线程结束的信息

        }
        #endregion

        #region ref
        public RelayCommand<object> RefCommand { get; set; }

        public void RefTest()
        {
            int a = 3;
            int b = 5;
            int c = 6;
            ConvertAB(ref a, ref b);
        }
        public void ConvertAB(ref int a, ref int b)
        {
            (b, a) = (a, b);
        }
        #endregion

        #region 多线程
        private static int[] arrs = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        public RelayCommand<object> ThreadCommand { get; set; }

        public object lockObj = new object();
        public void ThreadTest()
        {
            //Task printOddTask = Task.Run(() => PrintNumbers(true));
            //Task printEvenTask = Task.Run(() => PrintNumbers(false));

            //Task.WaitAll(printOddTask, printEvenTask);
            //Console.WriteLine("All numbers have been printed.");
            // 创建并启动一个后台线程
            //Thread backgroundThread = new Thread(new ThreadStart(DoWork));
            ////backgroundThread.IsBackground = true; // 设置为后台线程
            //backgroundThread.Start();

             // 创建并启动一个后台任务
        Task backgroundTask = Task.Factory.StartNew(() => DoWork(),TaskCreationOptions.LongRunning);
        Console.WriteLine("主线程继续执行...");

        // 注意：这里没有等待后台任务完成，程序将在主线程执行完毕后退出
        Console.WriteLine("主线程执行完毕，程序即将退出。");
            

            Console.WriteLine("主线程继续执行...");
            // 注意：这里没有等待后台线程完成，程序将在主线程执行完毕后退出
            Console.WriteLine("主线程执行完毕，程序即将退出。");

        }
        static void DoWork()
        {
            while (true)
            {
                Console.WriteLine("后台线程工作中...");
                Thread.Sleep(1000); // 模拟工作
            }
        }
        public int index=0;
        public void PrintNumbers(bool isOdd)
        {
            for (int i = 0; i < 10; i++)
            {
                lock (lockObj)
                {
                    if (index >= arrs.Length)
                        return; // 退出循环
                    if ((isOdd && arrs[i] % 2 != 0) || (!isOdd && arrs[i] % 2 == 0))
                    {
                        Console.WriteLine(index + " 数据" + arrs[index] + " " + $"{DateTime.Now.ToString("ss:fff")}");
                        index++;
                    }
                }
            }
            //while (true)
            //{
            //    lock (lockObj)
            //    {
            //        if (index >= arrs.Length)
            //            return; // 退出循环

            //        if ((isOdd && arrs[index] % 2 != 0) || (!isOdd && arrs[index] % 2 == 0))
            //        {
            //            Console.WriteLine(arrs[index]);
            //            index++; // 移动到下一个索引
            //            //if (isOdd) printOdd = false; // 如果打印的是奇数，下一个打印偶数
            //            //else printOdd = true; // 如果打印的是偶数，下一个打印奇数
            //        }
            //        //else
            //        //{
            //        //    // 当前索引的数不符合打印条件，释放锁，让另一个线程继续
            //        //    Monitor.Pulse(lockObj);
            //        //}
            //    }

            //    // 如果当前线程应该打印，但数组已经遍历完，则退出循环
            //    //if ((index >= arrs.Length) && (isOdd ? printOdd : !printOdd))
            //    //    return;
            //}

        }

        #endregion

        #region override
        public RelayCommand<object> BaseCommand => new RelayCommand<object>((b) => { BaseTest(); });

        public void BaseTest()
        {
            Class2 class2 = new Class2();
            BaseClass baseclass = (BaseClass)class2;
            baseclass.FuncA();
        }
        #endregion

        #region 登录
        private Person person=new Person();
        public Person Person
        { 
            get { return person; }
            set {  person = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 向上延伸
        private int topDis;
        public int TopDis
        {
            get { return topDis; }
            set { topDis = value; this.RaisePropertyChanged(nameof(TopDis)); }
        }
        private int rectHeight;
        public int RectHeight
        {
            get { return rectHeight; }
            set { rectHeight = value; this.RaisePropertyChanged(nameof(rectHeight)); }
        }
        public RelayCommand<object> TopDisCommand { get; set; }
        public RelayCommand<object> TopDownDisCommand { get; set; }

        #endregion
    }



    public class RelayCommand<T> : ICommand
    {
        public event EventHandler CanExecuteChanged;
        public Action<T> Action { get; }
        public RelayCommand(Action<T> action)
        {
            Action = action;
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            Action?.Invoke((T)parameter);
        }
    }
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class Human
    {
        public string Name;
        public int Age;
    }
    public class Person : ObservableObject
    {
        private string username;
        public string UserName
        {
            get {  return username; }
            set { username = value; RaisePropertyChanged(); }
        }
        private string password;
        public string Password
        {
            get { return password; }
            set { password = value; RaisePropertyChanged(); }
        }
    }

    public abstract class BaseClass
    {
        public virtual void FuncA()
        {
            Debug.WriteLine("这是基类方法");
        }
    }
    public class Class2 : BaseClass 
    {
        public override void FuncA()
        {
            Debug.WriteLine("这是子类方法");
        }
    }
    
}
